/**
 * 
 */
package junior.util.string;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;

/**
 * 字符串占位符工具类
 * <p>解析${xxx}类型的占位符</p>
 * @author WuBo
 * @CreateDate 2013-5-6 上午9:27:13
 */
public class StringPlaceHolderUtils {
	public final static String PLACE_HOLDER_PREFIX = "${";
	public final static String PLACE_HOLDER_SUFFIX = "}";
	/**
	 * 解析出所有占位符
	 * @author WuBo
	 * @CreateDate 2013-5-6 上午9:38:38
	 * @param s
	 * @return
	 */
	
	public static Set<String> getPlaceHolders(String s) {
		return getPlaceHolders(s, PLACE_HOLDER_PREFIX, PLACE_HOLDER_SUFFIX);
	}
	
	public static Set<String> getPlaceHolders(String s, String placeHolderPrefix, String placeHolderSuffix) {
        Set<String> phs = new HashSet<String>();
        parsePlaceHolders(s, placeHolderPrefix, placeHolderSuffix, placeHolder -> {
            phs.add(placeHolder);
        }, null);
        return phs;
	}
    
    public static void parsePlaceHolders(String s, String placeHolderPrefix, String placeHolderSuffix,
                                         Consumer<String> targetCollector, Consumer<String> otherStringCollector) {
        String placeHolder;
        int beginIndex = 0;
        while ((placeHolder = doGetPlaceHolder(s, beginIndex, placeHolderPrefix, placeHolderSuffix)) != null) {
            String sc = placeHolderPrefix+placeHolder+placeHolderSuffix;
            int index = s.indexOf(sc, beginIndex);
            if (otherStringCollector != null) {
                otherStringCollector.accept(s.substring(beginIndex, index));
            }
            targetCollector.accept(placeHolder);
            beginIndex = index + sc.length();
        }
        if (beginIndex == 0 && otherStringCollector != null) {
            otherStringCollector.accept(s);
        } else {
            if (beginIndex > 0 && beginIndex != s.length() && otherStringCollector != null) {
                otherStringCollector.accept(s.substring(beginIndex));
            }
        }
    }
	
	private static String doGetPlaceHolder(String s, int beginIndex, String placeHolderPrefix, String placeHolderSuffix) {
		String str = s.substring(beginIndex);
		int start = str.indexOf(placeHolderPrefix);
		if (start != -1) {
			int end = str.indexOf(placeHolderSuffix,start+1);
			if (end != -1) {
				return str.substring(start+placeHolderPrefix.length(), end);
			}
		}
		return null;
	}
	/**
	 * 以值替换占位符
	 * @author WuBo
	 * @CreateDate 2013-5-6 上午9:43:07
	 * @param s
	 * @param map
	 */
	public static String replace(String s, Map<String, ? extends Object> map) {
		return replace(s, map, PLACE_HOLDER_PREFIX, PLACE_HOLDER_SUFFIX);
	}

	public static String replace(String s, Map<String, ? extends Object> map, String placeHolderPrefix, String placeHolderSuffix) {
		String rslt = s;
		for (String key : map.keySet()) {
			Object value = map.get(key);
			if (value == null) value = "";
			rslt = replaceAll(rslt, placeHolderPrefix+key+placeHolderSuffix, value.toString());
		}
		return rslt;
	}
	
	public static String replaceAll(String s, String target, String replacement) {
		if (s.contains(target)) {
            if (target.equals(replacement)) {
                // prevent dead circle code
                return target;
            }
			return replaceAll(s.replace(target, replacement), target, replacement);
		}
		return s;
	}
	
}
